package com.zzzzzz.content.topic;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzzzzz.content.Constants;
import com.zzzzzz.content.ContentPictureService;
import com.zzzzzz.content.tag.TagService;

@Service("topicService")
public class TopicServiceImpl implements TopicService {

	@Resource
	private TopicDao topicDao;
	@Resource
	private TagService tagService;
	@Resource
	private ContentPictureService topicPictureService;
	
	/*@Transactional
	public Long add(Long userId, TopicForm topicForm) throws IOException {
		
		Date now = new Date();
		Topic topic = new Topic();
		topicForm.updateTopic(topic);
		topic.setUserId(userId);
		topic.setAddDt(now);
		topic.setUpdDt(now);
		topic.setStatus(0);
		
		if(topicForm.getFiles().getSize()>0){
			String picId = topicPictureService.saveTopicFacePicture(topicForm.getFiles());
			topic.setPicId(picId);
		}
		
		topicDao.add(topic);
		
		Long topicId = topic.getId();
		tagService.updateTagByAddTopic(topicId, topicForm.getTags());
		
		return topicId;
	}*/
	
	/*@Override
	public void del(Long topicId, Long userId) throws NoSuchTopicException {
		Topic topic = this.findById(topicId);
		topicDao.editStatus(topicId, 1, userId);
		tagService.updateTagByDelTopic(topicId, topic.getTags());
	}
	
	@Transactional
	public void edit(Long userId, Long topicId, TopicForm topicForm) throws IOException {
		Date now = new Date();
		
		Topic topic = new Topic();
		topic.setId(topicId);
		topicForm.updateTopic(topic);
		topic.setUserId(userId);
		topic.setUpdDt(now);
		
		if(topicForm.getFiles().getSize()>0){
			String picId = topicPictureService.saveTopicFacePicture(topicForm.getFiles());
			topic.setPicId(picId);
			topicDao.editPic(topicId, picId, userId);
		}
		
		topicDao.edit(topic);
		tagService.updateTagByEditTopic(topicId, topicForm.getTags());
	}
	
	public void plusLooks(Long topicId) {
		topicDao.editLooks(topicId, 1);
	}

	@Transactional
	public Integer like(Long topicId, Long userId) {
		try {
			topicFeelDao.add(new TopicFeel(topicId, userId, Constants.FEEL_TYPE.LIKE));
			topicDao.editLikes(topicId, 1);
		} catch (TopicFeelAlreadyExistsException e) {
			return Constants.FEEL_RESULT.ALREADY_FEEL;
		}
		return Constants.FEEL_RESULT.OK;
	}
	
	@Transactional
	public Integer unFeel(Long topicId, Long userId) {
		try {
			Integer feelType = topicFeelDao.findFeelType(topicId, userId);
			topicFeelDao.del(new TopicFeel(topicId, userId, feelType));
			if(Constants.FEEL_TYPE.LIKE == feelType){
				topicDao.editLikes(topicId, -1);
			}
			return Constants.FEEL_RESULT.OK;
		} catch (NoSuchTopicFeelException e) {
			return Constants.FEEL_RESULT.NOSUCH_FEEL;
		}
	}
	
	public void plusReplies(Long topicId, Long userId) {
		topicDao.editReplies(topicId, 1);
	}
	
	public Topic findById(Long topicId) throws NoSuchTopicException {
		return topicDao.findById(topicId);
	}
	
	public TopicView findTopicViewById(Long topicId) throws NoSuchTopicException {
		return topicDao.findTopicViewById(topicId);
	}
	
	public void editStatus(Long id, Integer status, Long userId) {
		topicDao.editStatus(id, status, userId);
	}
	*/

}
